package com.woniuxy.house.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.entity.house.Renthouse;
import com.woniuxy.entity.house.dto.RenthouseDto;
import com.woniuxy.entity.house.dto.RenthouseQO;
import com.woniuxy.house.service.RenthouseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/house/renthouse")
public class RenthouseController {

    @Resource
    private RenthouseService renthouseService;

    /**
     * 新增租房信息
     */
    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Renthouse renthouse) {
        return renthouseService.save(renthouse) ?
                new ResponseResult<>(500,"新增租房成功！") :
                new ResponseResult<>(200,"新增租房失败！");
    }

    /**
     * 分页查询所有租房信息
     */
    @GetMapping("/page/{current}/{size}")
    public ResponseResult<Page<Renthouse>> page(@PathVariable int current, @PathVariable int size) {
        return new ResponseResult<>(renthouseService.findPage(current, size));
    }

    /**
     * 通过租房id查询到户型信息，配套信息，楼盘信息，对应三个经纪人名称，头像
     */
    @GetMapping("/{id}")
    public ResponseResult<RenthouseDto> findOneById(@PathVariable int id) {
        RenthouseDto renthouseDto = renthouseService.findOneById(id);
        return renthouseDto != null ?
                new ResponseResult<>(renthouseDto) :
                new ResponseResult<>(500, "未查询到数据");
    }

    /**
     * 根据租房id软删除租房信息，将status修改为1
     */
    @PostMapping("/{id}")
    public ResponseResult<Void> deleteOneById(@PathVariable int id) {
        return renthouseService.deleteOneById(id) ? new ResponseResult<>(200,"删除成功！")
                : new ResponseResult<>(500,"删除失败！");
    }

    /**
     * 根据租房id修改单个租房信息
     */
    @PostMapping("/update")
    public ResponseResult<Void> update(@RequestBody Renthouse renthouse) {
        return renthouseService.updateByRid(renthouse) ? new ResponseResult<>(200,"修改成功！")
                : new ResponseResult<>(500,"修改失败！");
    }

    /**
     * 根据条件分页查询（包括排序）
     */
    @GetMapping("/findLike/{current}/{size}")
    public ResponseResult<Page<RenthouseDto>> findLike(@RequestBody RenthouseQO renthouseQO, @PathVariable int current, @PathVariable int size) {
        Page<RenthouseDto> pageLike = renthouseService.findLike(current,size,renthouseQO);
        return pageLike != null ?
                new ResponseResult<>(pageLike) :
                new ResponseResult<>(500,"未查询到数据");
    }
}

